package com.bj58.mis.apm.server.kylin.common;

import com.bj58.mis.apm.server.kylin.common.hllc.HLLCounter;
import com.bj58.mis.apm.server.kylin.common.hllc.HadoopUtil;
import com.bj58.mis.apm.server.kylin.common.util.ByteArray;
import com.bj58.mis.apm.server.kylin.common.util.Bytes;
import com.google.common.collect.Maps;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.util.ReflectionUtils;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;

public class CubeStatsResult {

    private int percentage = 100;
    private double mapperOverlapRatio = 0.0D;
    private int mapperNumber = 0;
    private Map<Long, HLLCounter> counterMap = Maps.newHashMap();

    public CubeStatsResult(String seqfile, int precision) throws IOException {
        Configuration hadoopConf = HadoopUtil.getCurrentConfiguration();
        Path path = new Path(HadoopUtil.fixWindowsPath("file://" + seqfile));
        SequenceFile.Reader.Option seqInput = SequenceFile.Reader.file(path);
        SequenceFile.Reader reader = new SequenceFile.Reader(hadoopConf, new SequenceFile.Reader.Option[]{seqInput});
        Throwable var6 = null;

        try {
            LongWritable key = (LongWritable)ReflectionUtils.newInstance(reader.getKeyClass(), hadoopConf);
            BytesWritable value = (BytesWritable)ReflectionUtils.newInstance(reader.getValueClass(), hadoopConf);

            while(reader.next(key, value)) {
                if (key.get() == 0L) {
                    this.percentage = Bytes.toInt(value.getBytes());
                } else if (key.get() == -1L) {
                    this.mapperOverlapRatio = Bytes.toDouble(value.getBytes());
                } else if (key.get() == -2L) {
                    this.mapperNumber = Bytes.toInt(value.getBytes());
                } else if (key.get() > 0L) {
                    HLLCounter hll = new HLLCounter(precision);
                    ByteArray byteArray = new ByteArray(value.getBytes());
                    hll.readRegisters(byteArray.asBuffer());
                    this.counterMap.put(key.get(), hll);
                }
            }
        } catch (Throwable var18) {
            var6 = var18;
            throw var18;
        } finally {
            if (reader != null) {
                if (var6 != null) {
                    try {
                        reader.close();
                    } catch (Throwable var17) {
                        var6.addSuppressed(var17);
                    }
                } else {
                    reader.close();
                }
            }

        }

    }

    public int getPercentage() {
        return this.percentage;
    }

    public double getMapperOverlapRatio() {
        return this.mapperOverlapRatio;
    }

    public int getMapperNumber() {
        return this.mapperNumber;
    }

    public Map<Long, HLLCounter> getCounterMap() {
        return Collections.unmodifiableMap(this.counterMap);
    }
    
}
